﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Research.BLL;
using System.Transactions;

namespace Research.Web
{
    public partial class RprjData : System.Web.UI.Page
    {
        readonly BasicBll _bllBasic = new BasicBll();

        readonly RprjMainBll _bllRprjMain = new RprjMainBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
                BindgridRprjData(Convert.ToInt16(Request["RprjID"]), Convert.ToInt16(Request["Type"]));
            //}
            
        }

        private void BindgridRprjData(int rPrjID,int dataTypeID)
        {
            using (var ds = _bllBasic.GetRprjData(rPrjID, dataTypeID))
            {
                gridRprjData.DataSource = ds;
                gridRprjData.DataBind();
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            using (var ts = new TransactionScope())
            {
                try
                {
                    var dataPath = Request["RprjID"] + "_" + Request["Type"] + "_" +
                                   DateTime.Now.Ticks.ToString() + System.IO.Path.GetExtension(fupldData.FileName);

                    _bllRprjMain.WriteUploadData(Convert.ToInt32(Request["RprjID"]), Convert.ToInt32(Request["Type"]),
                                                 txtDataName.Text, dataPath, DateTime.Now.ToString("yyyy-MM-dd"));
                    fupldData.SaveAs(Server.MapPath("~/Upload/" + dataPath));

                    ts.Complete();
                    ExtAspNet.Alert.Show("上传成功！");
                }
                catch (Exception)
                {
                    {
                        ExtAspNet.Alert.Show("上传失败！");
                    }
                    throw;
                }
                finally
                {
                    ts.Dispose();
                    BindgridRprjData(Convert.ToInt16(Request["RprjID"]), Convert.ToInt16(Request["Type"]));
                }
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHideRefreshReference());
        }


    }
}